package mtons.cast.modules.play.ticket.strategy;

import com.alibaba.fastjson.JSONArray;
import mtons.cast.modules.play.ticket.Ticket;

import java.util.Map;

/**
 * Created by langhsu on 2017/10/12.
 */
public class MtonsTicketStrategy extends AbstractStrategy implements Ticket {
    static String[] keys = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};

    @Override
    public String produce(boolean prevResult, String round) {
        Map<Integer, JSONArray> logs = maker.getHistory();
        Integer max = 0;
        for (Map.Entry<Integer, JSONArray> entry : logs.entrySet()) {
            if (entry.getKey() > max) {
                max = entry.getKey();
            }
        }
        JSONArray numbers = logs.get(max - 1);
        if (numbers != null) {
            int s = 0;

            for (int i = 0; i < numbers.size(); i++) {
                s += Integer.parseInt(numbers.get(i).toString());
            }

            int sum = (s / 10 + s % 10);

            int left = (sum - 1);
            if (left < 0) {
                left = 9;
            }

            int right = (sum + 1);
            if (right > 9) {
                right = 0;
            }

            String ret = s + "" + (sum % 10) + keys[left] + keys[right];
//            String ret = s + "" + (sum > 9 ? 0 : sum) + keys[left] + keys[right];

            System.out.println("分析号码:" + ret);
            return ret;
        }

        return null;
    }

}
